package com.yoomiito.app.ui.account;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.InputFilter;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.jakewharton.rxbinding3.widget.RxTextView;
import com.yoomiito.app.base.App;
import com.yoomiito.app.base.BaseActivity;
import com.yoomiito.app.R;

import com.yoomiito.app.model.bean.InvitePeopleInfoBean;
import com.yoomiito.app.model.bean.UserInfo;
import com.yoomiito.app.present.account.InvitationCodePresent;
import com.yoomiito.app.utils.AllCode;
import com.yoomiito.app.utils.EventMessage;
import com.yoomiito.app.utils.L;
import com.yoomiito.app.utils.LoginUtils;
import com.yoomiito.app.utils.UserInfoConfig;
import com.yoomiito.app.utils.YMToast;
import com.yoomiito.app.utils.inputfilter.InviteCodeInputFilter;
import com.yoomiito.app.widget.dialog.CustomDialog;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 绑定邀请码
 */
public class InvitationCodeActivity extends BaseActivity<InvitationCodePresent> {

    @BindView(R.id.et_invite_code)
    EditText et_invite_code;
    @BindView(R.id.binding)
    Button binding;

    @BindView(R.id.binding1)
    Button binding2;
    private CustomDialog mCustomDialog;
    private String invite_code;//邀请码
    private String mPhone;
    private String binding1;

    public static void start(Context context, String phone, String binding) {
        Intent intent = new Intent(context, InvitationCodeActivity.class);
        intent.putExtra("phone", phone);
        intent.putExtra("binding", binding);
        context.startActivity(intent);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_invitation_code;
    }

    @Override
    public InvitationCodePresent newP() {
        return new InvitationCodePresent(App.mAppConfig);
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        initView();
    }

    private void initView() {
        mPhone = getIntent().getStringExtra("phone");
        binding1 = getIntent().getStringExtra("binding");
//        et_invite_code.setFilters(new InputFilter[]{new InviteCodeInputFilter().setLength(11)});
        RxTextView.textChanges(et_invite_code).subscribe(charSequence -> binding.setEnabled(charSequence.length() >= 6));
        if (TextUtils.equals(binding1, "2")) {
            binding2.setVisibility(View.VISIBLE);
        } else {
            binding2.setVisibility(View.GONE);
        }

    }

    @Override
    public void eventMessage(EventMessage eventMessage) {
        super.eventMessage(eventMessage);
        if (AllCode.LoginSuccess.equals(eventMessage.getEventType())) {
            this.finish();
        }
    }

    /**
     * 立即绑定 获取邀请人信息
     *
     * @param infoBean
     */
    public void success(InvitePeopleInfoBean infoBean) {
        dismissLoading();
        showInvitePersonInfo(TextUtils.isEmpty(infoBean.getNickname()) ? infoBean.getMobile() : infoBean.getNickname(),
                infoBean.getHeadimgurl(), infoBean.getIvcode());
    }

    /**
     * 确认绑定邀请人成功 跳转主界面 不再存token
     */
    public void bindSuccess(UserInfo userInfo) {
        dismissLoading();
//        CurrentUser.mUserInfo = userInfo;
//        SharedPref.getInstance(App.getInstance()).putBoolean(Constant.IS_LOGIN, true);
//        SharedPref.getInstance(App.getInstance()).putString(Constant.TOKEN, userInfo.getToken());

//        UserInfoConfig.loginPutUserConfig(userInfo,true);
//        EventMessage em = new EventMessage();
//        em.setEventType(AllCode.LoginSuccess);
//        EventBus.getDefault().post(em);
        userInfo.setToken(userInfo.getToken());//以校验验证码时给得token为准
        LoginUtils.INSTANCE.loginHandle(this, userInfo);
    }

    /**
     * 展示邀请人信息
     *
     * @param mNickname
     * @param mHeadImgUrl
     */
    private void showInvitePersonInfo(String mNickname, String mHeadImgUrl, String code) {
        if (mCustomDialog == null) {
            mCustomDialog = new CustomDialog(this, () -> {
                showLoading();
                getP().requestBindInvitePerson(mPhone, invite_code);
            });
        }
        mCustomDialog.setContent(mNickname);
        mCustomDialog.setTitle(getString(R.string.Invite_people));
        mCustomDialog.setHead(mHeadImgUrl);
        mCustomDialog.show();
    }

    @OnClick({R.id.binding, R.id.binding1})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.binding:
                invite_code = et_invite_code.getText().toString().trim();
                if (TextUtils.isEmpty(invite_code)) {
                    YMToast.showText("请输入邀请码");
                    return;
                }
                showLoading();
                getP().requestInvitePeopleInfo(invite_code);
                break;
            case R.id.binding1:
                getP().requestBindInvitePerson(mPhone, "18888888888");
                break;
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mCustomDialog = null;
    }

}
